/**
 * Created by Administrator on 2017/6/19.
 */
var config = require('../../config.json');
var User = require('../models/user');
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');

exports.init = function () {
  if (config.initDB) {
    //进行初始化
    var superamdin = {
      name: 'superadmin',
      password: crypto.createHash('md5').update('superadmin').digest('hex'),
      role: 2,
      time: Date.now()
    };
    var newSuperadmin = new User(superamdin);
    newSuperadmin.save(function (err, _user) {
      if (err) console.log('初始化DB进行录入user：', err);
    });

    var amdin = {
      name: 'admin',
      password: crypto.createHash('md5').update('admin').digest('hex'),
      role: 1,
      time: Date.now()
    };
    var newAdmin = new User(amdin);
    newAdmin.save(function (err, _user) {
      if (err) console.log('初始化DB进行录入user：', err);
    });

    //修改配置，下次就不需要再进行初始化了
    config.initDB = false;
    var file = path.join(__dirname, '../../config.json');
    fs.writeFile(file, JSON.stringify(config, null, 4), function (err) {
      if (err) {
        console.log(err);
      } else {
        console.log("JSON saved to " + file);
      }
    });
  }
}



